package handler

import (
	"cargo-admin/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func UserAddRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}

func UserRoleList(c *gin.Context) {
	uid := c.Query("uid")
	var role model.Role
	/*
		SELECT
		*
		FROM
		roles
		LEFT JOIN user_roles ON user_roles.rid=roles.id
		WHERE user_roles.uid=1
	*/
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid=roles.id").
		Where("user_roles.uid = ?", uid).
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": role,
	})
	return
}
